$(function () {
    /*=========方法的调用==============*/
    init();
    // 初始化
    function init() {
        getId();
        getById();
        getByIdOfRank();
        getCommentById()
    }

    // 获取地址栏URl中的id,
    var id;

    function getId() {
        id = parseInt(location.search.substring(4));
        // console.log(id);
    }


    //传入id获取品牌热度数据
    function getById() {
        $.ajax({
            type: "get",
            url: "http://193.112.55.79:9090/api/getbrand",
            data: {
                brandtitleid: 0
            },
            dataType: "json",
            success: function (res) {
                // console.log(res);
                let html = template("commodity", res);
                $(".commodity").html(html);
            }
        });
    }

    //传入id获取产品销量排行数据
    function getByIdOfRank() {
        $.ajax({
            type: "get",
            url: "http://193.112.55.79:9090/api/getbrandproductlist",
            data: {
                brandtitleid: id
            },
            dataType: "json",
            success: function (res) {
                // console.log(res);
                let html = template("brandList", {
                    list: res.result
                });
                $(".brandList").html(html);
            }
        });
    }

    //传入id获取商品评论
    console.log(id)

    function getCommentById() {
        $.ajax({
            type: "get",
            url: "http://193.112.55.79:9090/api/getproductcom",
            data: {
                productid: id
            },
            dataType: "json",
            success: function (res1) {
                $.ajax({
                    type: "get",
                    url: "http://193.112.55.79:9090/api/getbrandproductlist",
                    data: {
                        brandtitleid: id
                    },
                    dataType: "json",
                    success: function (res2) {
                        console.log(res2);

                        for (var i = 0; i < res1.result.length; i++) {
                            res1.result[i].productImg = res2.result[0].productImg;
                        }

                        console.log(res1);
                        let html = template("comment", {
                            comment: res1.result
                        });
                        $(".product-com ul").html(html);
                    }
                });

            }
        });
    }
})